<!-- 投标结果申请 -->
<template>
	<view class="page">
		<u--form
			labelPosition="top"
			:model="model"
			ref="ErpOmcProjectApplyForm"
			:rules="rules"
			labelWidth="auto"
			:labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}"
		>
			<view class="basic-style">
				<u-form-item
					label="单据编号"
					prop="bidResultNo"
					:borderBottom="false"
					required
				>
					<u--input
						disabled
						placeholder="请输入单据编号"
						v-model="model.bidResultNo"
					></u--input>
				</u-form-item>
				<u-form-item
					label="日期"
					prop="createTime"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.createTime"
						disabled
						placeholder="请选择日期"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				<u-form-item
					label="申请人"
					prop="proposer"
					:borderBottom="false"
					required
				>
					<h-select-person-input
						style="width: 100%"
						disabled
						v-model="model.proposer"
						placeholder="请选择申请人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.proposer"
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="申请部门"
					prop="proposerDeptId"
					:borderBottom="false"
					required
				>
					<h-select-depart-input
						disabled
					  v-model="model.proposerDeptId"
					  :departName="model.proposerDeptText"
					  placeholder="请选择部门"
					  border="surround"
					  style="width: 100%"
					  suffixIcon="arrow-down-fill"
					  :suffixIconStyle="{ color: '#B2B2B2' }"
					  :title.sync="model.proposerDeptText"
					  @detail="ontenderDeptDetail"
					></h-select-depart-input>
				</u-form-item>
				<view class="basic-title-center">
					<u-line :length="120"></u-line>
					<view class="">基本信息</view>
					<u-line :length="120"></u-line>
				</view>
				<u-form-item
					label="关联投标档案"
					prop="tenderId"
					:borderBottom="false"
					required
					@click="prepareProjectClick"
				>
					<h-select-omc-project-tender-input
						v-model="model.tenderId"
						style="width: 100%"
						apiListUrl="/api/erp/omc/tender/erpOmcTenderFinal/queryByIdContainTenderFile"
						placeholder="请选择关联投标档案"
						objectField="model.tenderId"
						:dataType="'emp'"
						:dataIsolation="'tender_person'"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						@input="tenderFinalChange"
					></h-select-omc-project-tender-input>
				</u-form-item>
				<u-form-item
					label="项目名称"
					prop="projectName"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入项目名称"
						v-model="model.projectName"
					></u--input>
				</u-form-item>
				<u-form-item
					label="项目承接人"
					prop="undertaker"
					:borderBottom="false"
					required
				>
					<h-select-person-input
						style="width: 100%"
						v-model="model.undertaker"
						placeholder="请选择项目承接人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.undertaker"
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="业绩归属年度"
					prop="year"
					:borderBottom="false"
					required
				>
					<h-dict-select
						style="width: 100%" 
						v-model="model.year" 
						placeholder="请选择业绩归属年度" 
						dictName="yjgsnd" 
						suffixIcon="arrow-down-fill" 
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="项目地址"
					prop="projectAddress"
					:borderBottom="false"
					required
				>
					<h-select-address-input
						style="width: 100%"
						v-model="model.projectAddress"
						placeholder="请选择项目地址"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-select-address-input>
				</u-form-item>
				<u-form-item
					label="项目详细地址"
					prop="detailedAddress"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入项目详细地址"
						v-model="model.detailedAddress"
					></u--input>
				</u-form-item>
				<u-form-item
					label="经营团队"
					prop="businessTeam"
					:borderBottom="false"
					required
				>
					<h-select-team-input
					  @input="changeBusinessCompany"
						style="width: 100%;"
						v-model="model.businessTeam"
						objectField="model.businessTeam"
						placeholder="请选择经营团队"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						:tdlx="1"
					></h-select-team-input>
				</u-form-item>

				<!-- <u-form-item
				label="经营公司"
				prop="businessCompanyId"
				:borderBottom="false"
				required>
				<h-select-company-input
					disabled
					style="width: 100%"
					v-model="model.businessCompanyId"
					border="surround"
					placeholder="请选择经营公司"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
					objectField = 'managerTrusteeForm.gsdaId'
				></h-select-company-input>
              </u-form-item> -->
				<u-form-item
					label="开标时间"
					prop="bidOpeningData"
					:borderBottom="false"
					required
				>
					<h-time-select
						v-model="model.bidOpeningData"
						placeholder="请选择开标时间"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						mode="datetime"
					></h-time-select>
				</u-form-item>
				
				<u-form-item
					label="招标方式"
					prop="biddingMethod"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-dict-select
						style="width: 100%" 
						v-model="model.biddingMethod" 
						placeholder="请选择招标方式" 
						dictName="bidding_method"
						suffixIcon="arrow-down-fill" 
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="业主单位"
					prop="ownerUnit"
					:borderBottom="false"
					required
					@click="onCustomerClick"
				>
					<h-select-customer-input
						style="width: 100%"
						v-model="model.ownerUnit"
						commercialType= "1"
						border="surround"
						placeholder="请选择业主单位"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField = 'model.ownerUnit'
						@input="onCustomerChange"
					></h-select-customer-input>
				</u-form-item>
				<u-form-item
					label="联系人"
					prop="ownerLinkman"
					:borderBottom="false"
					required
				>
					<h-dict-select
						style="width: 100%" 
						v-model="model.ownerLinkman" 
						placeholder="请选择业主联系人" 
						:dictName="cust_lxr"
						suffixIcon="arrow-down-fill" 
						:suffixIconStyle="{color: '#B2B2B2'}"
						@change="lxrChange"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="联系方式"
					prop="ownerTel"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.ownerTel"
						placeholder="请输入业主联系方式"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="招标代理机构"
					prop="tenderAgent"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.tenderAgent"
						placeholder="请输入招标代理机构"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="招标代理联系人"
					prop="tenderAgentLinkman"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.tenderAgentLinkman"
						placeholder="请输入招标代理联系人"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="招标代理联系方式"
					prop="tenderAgentTel"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.tenderAgentTel"
						placeholder="请输入招标代理联系方式"
						border="surround"
					></u--input>
				</u-form-item>
				
				<u-form-item
					label="项目公示信息"
					prop="projectPublicity"
					:borderBottom="false"
					ref="item1"
				>
					<u--textarea
						v-model="model.projectPublicity"
						confirmType="done"
						placeholder="请输入项目公示信息"
						:maxlength="500"
					></u--textarea>
				</u-form-item>
				<u-form-item
					label="投标内容概要"
					prop="tenderContent"
					:borderBottom="false"
					ref="item1"
				>
					<u--textarea
						v-model="model.tenderContent"
						confirmType="done"
						placeholder="请输入投标内容概要"
						:maxlength="500"
					></u--textarea>
				</u-form-item>
				<!-- 附件 -->
				<view class="">
					<view class="basic-title-center">
						<u-line :length="120"></u-line>
						<view class="">招标文件</view>
						<u-line :length="120"></u-line>
					</view>
					<view class="warning">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<view class="" style="width: 100%;">
						<h-upload-file v-model="model.tenderFile" :isIcon="false" ></h-upload-file>
						<file-list v-if="model.tenderFile" v-model="model.tenderFile" :isDelete="true"></file-list>
					</view>
				</view>
				<u-form-item
					label="招标控制价(元)"
					prop="controlPrice"
					:borderBottom="false"
					ref="item1"
				>
					<h-money-input
						placeholder="请输入招标控制价"
						v-model="model.controlPrice"
					></h-money-input>
				</u-form-item>
				<u-form-item
					type="number"
					label="招标代理费(元)"
					prop="agentFee"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-money-input
						placeholder="请输入招标代理费"
						v-model="model.agentFee"
					></h-money-input>
				</u-form-item>
				<u-form-item
					label="履约保证金形式"
					prop="bailType"
					:borderBottom="false"
				>
					<h-dict-select
						style="width: 100%" 
						v-model="model.bailType" 
						placeholder="请选择履约保证金形式" 
						dictName="bail_type"
						suffixIcon="arrow-down-fill" 
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				
				<u-form-item
					label="履约保证金(元)"
					prop="bail"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-money-input
						placeholder="请输入履约保证金"
						v-model="model.bail"
					></h-money-input>
				</u-form-item>
				<u-form-item
					label="履约保证金退还日期"
					prop="bailReturnData"
					:borderBottom="false"
					ref="item1"
				>
					<h-time-select
						v-model="model.bailReturnData"
						placeholder="请选择履约保证金退还日期"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						mode="date"
					></h-time-select>
				</u-form-item>
				<!-- 附件 -->
				<view class="">
					<view class="basic-title-center">
						<u-line :length="120"></u-line>
						<view class="">归档资料</view>
						<u-line :length="120"></u-line>
					</view>
					<view class="warning">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<view class="" style="width: 100%;">
						<h-upload-file v-model="model.archivesFile" :isIcon="false" ></h-upload-file>
						<file-list v-if="model.archivesFile" v-model="model.archivesFile" :isDelete="true"></file-list>
					</view>
				</view>
			</view>
			<view class="basic-style">
				<view class="basic-title-center">
					<u-line :length="80"></u-line>
					<view class="">投标单位信息(内部)</view>
					<u-line :length="80"></u-line>
				</view>
				<u-form-item
					label="投标单位"
					prop="tenderUnit"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-select-company-input
					  style="width: 100%"
					  v-model="model.tenderUnit"
					  border="surround"
					  placeholder="请选择投标单位"
					  suffixIcon="arrow-down-fill"
					  :suffixIconStyle="{ color: '#B2B2B2' }"
					  objectField="model.tenderUnit"
					></h-select-company-input>
				</u-form-item>
				<u-form-item
					label="项目负责人"
					prop="projectLeading"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="model.projectLeading"
						placeholder="请输入项目负责人"
						border="surround"
					></u--input>
					<!-- <h-select-person-input
						style="width: 100%"
						v-model="model.projectLeading"
						placeholder="请选择项目负责人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.projectLeading"
					></h-select-person-input> -->
					
				</u-form-item>
				<u-form-item
					label="投标金额(元)"
					prop="tenderAmount"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-money-input
						placeholder="请输入投标金额"
						v-model="model.tenderAmount"
					></h-money-input>
				</u-form-item>
				<u-form-item
					label="中标结果"
					prop="bidResult"
					:borderBottom="false"
					ref="item1"
					required
				>
					<h-dict-select
						style="width: 100%" 
						v-model="model.bidResult" 
						placeholder="请选择中标结果" 
						dictName="bid_result"
						suffixIcon="arrow-down-fill" 
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="难度系数"
					prop="complexCoefficient"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						placeholder="请输入难度系数"
						v-model="model.complexCoefficient"
					></u--input>
				</u-form-item>
				<u-form-item
					label="成果系数"
					prop="resultCoefficient"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						placeholder="请输入成果系数"
						v-model="model.resultCoefficient"
					></u--input>
				</u-form-item>
				<u-form-item
					label="说明"
					prop="instruction"
					:borderBottom="false"
					ref="item1"
				>
					<u--textarea
						v-model="model.instruction"
						confirmType="done"
						placeholder="请输入说明"
						:maxlength="500"
					></u--textarea>
				</u-form-item>
			</view>
			
			<view class="basic-style">
				<view class="basic-title-center">
					<u-line :length="80"></u-line>
					<view class="">投标单位信息(外部)</view>
					<u-line :length="80"></u-line>
				</view>
				<ErpOmcTenderResultUnitForm
					@ok="onInforList" 
					ref="projectTaskFormConfig" 
					:assignedAmount="model.controlPrice" 
					@resetStatus="resetStatus"
					:bidResult="model.bidResult"
					:dataList="dataSource"
				></ErpOmcTenderResultUnitForm>
			</view>
			
			<view class="space-x-1 px-1 btn_position">
				<u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="onDrafts"></u-button>
				<u-button :disabled="isSubmit"  type="primary"  text="保存并提交" @click="onSubmit"></u-button>
			</view>
			<view style="height: 100rpx;"></view>
		</u--form>
		
		<u-toast ref="uToast"></u-toast>
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
	</view>
</template>
<script>
import fileList from '@/pages/synergy/components/fileList.vue'
import ErpOmcTenderResultUnitForm from './components/ErpOmcTenderResultUnitForm.vue' // 业务分类组件
import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
import http from '@/utils/request/index.js' // 请求方式
import {
	generatedFormNumber, // 获取单据编号
	erpOmcCustomerManagerContactId, // 获取联系方式
	erpOmcTenderResultAdd, // 添加
	erpOmcTenderResultEdit, // 编辑
	erpOmcTenderResultQueryById, // 详情
	queryByIdTenderResultList, // 获取子表
	
	queryTenderListByTenderId, // 文件審核子表
} from '@/api/index.js'
import moment from 'moment'
import {
	BASE_URL
} from '@/utils/baseUrl.js'
import { convertCurrency } from '@/utils/MoneyConvert' // 金额大写转换
var projectId = ''
export default {
	components: {
		fileList,
		ErpOmcTenderResultUnitForm,
		ProcessNodeConfiguration
	},
	data() {
		return {
			isFinalProjectId: false, // 用于判断编辑的时候不执行投标档案详情
			isCustomerId: false, // 选择业主单位才会把联系人和联系方式置空
			isSubmit: false,
			model: {
				bidResultNo: '', //单据编号
				createTime: '', // 申请时间
				proposer: '', // 申请人
				businessCompanyId: '', // 经营公司
				proposerDeptId: '', // 申请部门
				tenderId: '', // 投标档案
				projectName: '', // 项目名称
				undertaker: '', // 项目承接人
				year: '', // 业绩发归属年度
				projectAddress: '', // 项目所在地址
				detailedAddress: '', // 项目详细地址
				businessTeam: '', // 经营团队
				bidOpeningData: '', // 开标时间
				biddingMethod: '', // 招标方式
				
				ownerUnit: '', // 业主单位
				ownerLinkman: '', // 业主联系人
				ownerTel: '', // 业主联系方式
				tenderAgent: '', // 招标代理机构
				tenderAgentLinkman: '', //招标代理联系人
				tenderAgentTel: '', // 招标代理联系方式
				projectPublicity: '', // 项目公式信息
				tenderFile: '', // 招标文件
				controlPrice: '', // 招标控制价
				agentFee: '', // 招标代理费
				bailType: '', // 履约保证金形式
				bail: '', // 履约保证金
				bailReturnData: '', // 履约保证金退还日期
				archivesFile: '', // 归档资料
				tenderUnit: '', // 招标单位
				projectLeading: '', // 项目负责人
				tenderAmount: '', // 投标金额
				bidResult: '', // 投标结果
				complexCoefficient:'', // 难度系数
				resultCoefficient:'', // 成果系数
				instruction:'', // 说明
			},
			cust_lxr: 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=0',
			id: '', // 详情id值
			dataSource: [], // 业务分类信息列表
			rules: {
				bidResultNo: [{ required: true, message: '请输入单据编号!', trigger: ['blur', 'change'] }],
				proposerDeptId: [{ required: true, message: '请选择申请部门!', trigger: ['blur', 'change'] }],
				proposer: [{ required: true, message: '请选择申请人!', trigger: ['blur', 'change'] }],
				createTime: [{ required: true, message: '请选择申请时间!', trigger: ['blur', 'change'] }],
				
				tenderId: [
				   { required: true, message: '请选择关联投标档案!', trigger: ['change','blur'] },
				],
				projectName: [
					{ required: true, message: '请输入项目名称!', trigger: ['change','blur'], },
					{
						asyncValidator: (rule, value, callback) => {
							let params = {
								projectName: value,
								projectId: projectId,
								type: 'tenderResult' // 立项：project  预立项：prepare 投标文件审核：tenderFile  投标结果申请：tenderResult
							}
							http.get('/api/erp/omc/project/erpOmcProject/checkProjectDuplicate', params).then(res => {
								// 如果验证不通过，需要在callback()抛出new Error('错误提示信息')
								if (res.success) {
									callback();
								} else {
									callback(new Error(res.message));
								}
							}).catch(err => {
								callback(new Error(err.message));
							})
						},
						trigger: ['blur']
					},
				],
				undertaker: [
					{ required: true, message: '请选择承接人!', trigger: ['change','blur'], },
				],
				year: [
					{ required: true, type: 'number',  message: '请选择业绩归属年度!', trigger: ['change','blur'], },
				],
				
				projectAddress: [
				  { required: true, message: '请选择项目地址!', trigger: ['change','blur'], },
				],
				detailedAddress: [
				  { required: true, message: '请输入项目详细地址!', trigger: ['change','blur'], },
				],
				businessTeam: [
				  { required: true, message: '请选择经营团队!', trigger: ['change','blur'], },
				],
				bidOpeningData: [
					{ required: true, message: '请选择开标时间!', trigger: ['change','blur'], }
				],
				
				ownerUnit: [
				  { required: true, message: '请选择业主单位!', trigger: ['change','blur'], },
				],
				ownerLinkman: [
				  { required: true, message: '请选择业主联系人!', trigger: ['change','blur'], },
				],
				ownerTel: [
					{ required: true, message: '请输入业主联系方式！', trigger: ['change', 'blur'], },
					{ required: true, pattern: /(^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$)|(^\/$)/, message: '请输入正确的联系方式!', trigger: ['change', 'blur'] }
				],
				
				tenderAgent: [
					{ required: true, message: '请输入招标代理机构!', trigger: ['change','blur'] }
				],
				tenderAgentLinkman: [
					{ required: true, message: '请输入招标代理联系人!', trigger: ['change','blur'] }
				],
				tenderAgentTel: [
					{ required: true, message: '请输入招标代理联系方式！', trigger: ['change', 'blur'], },
					{ required: true, pattern: /(^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$)|(^\/$)/, message: '请输入正确的联系方式!', trigger: ['change', 'blur'] }
				],
				
				biddingMethod: [
					{ required: true, message: '请选择招标方式!', trigger: ['change', 'blur'] }
				],
				tenderUnit: [
					{ required: true, message: '请选择投标单位!', trigger: ['change','blur'], },
				],
				
				controlPrice: [
					{
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							if (value == '') return true
							return uni.$u.test.amount(value);
						},
						message: '请输入正确的金额!',
						// 触发器可以同时用blur和change
						trigger: ['blur']
					}
				],
				bidResult: [
						{ required: true, message: '请选择中标结果!', trigger: ['change','blur'] }
				],
				complexCoefficient: [
					{ required: true, message: '请输入难度系数!', trigger: ['change','blur'] }
				],
				resultCoefficient: [
					{ required: true, message: '请输入成果系数!', trigger: ['change','blur'] }
				],
				tenderAmount: [
					{
						type: 'number',
						required: true, message: '请输入投标金额!',
						trigger: ['change','blur']
					},
					{
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							return uni.$u.test.amount(value);
						},
						message: '请输入正确的金额!',
						// 触发器可以同时用blur和change
						trigger: ['blur']
					}
				],
				agentFee: [
					{ required: true, message: '请输入招标代理费!', trigger: ['change','blur'] },
					{
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							if (!value) return true
							return uni.$u.test.amount(value);
						},
						message: '请输入正确的金额!',
						// 触发器可以同时用blur和change
						trigger: ['blur']
					}
				],
				bail: [
					{ required: true, message: '请输入履约保证金!', trigger: ['change','blur'] },
					{
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							if (!value) return true
							return uni.$u.test.amount(value);
						},
						message: '请输入正确的金额!',
						// 触发器可以同时用blur和change
						trigger: ['blur']
					}
				]
			},
			
			//代码生成后需手动设置流程编码
			flowCode: 'dev_erp_omc_tender_result_001',
			formUrl: 'erp/omc/tenderresult/modules/ErpOmcTenderResultForm',
			beanName: 'erpOmcTenderResultServiceImpl',
			formUrlMobile: '/pages/synergy/erpOmcTenderResultList',
			processId: '1478570287271993345',
		};
	},
	
	computed: {
	    // 投标审核需要动态禁用的状态字段
	    editFildDisabled() {
	      // 新增和变更的时候只能看不能填
	      return true
	    },
	  },
	onLoad(options) {
		this.id = options.id ||  ''
		if (options.id) {
			// 编辑
			this.edit(options.id)
		} else {
			// 添加
			this.add()
		}
		
	},
	methods: {

		changeBusinessCompany(val){
      this.model.businessCompanyId = val.belongCompanyId
      },
		// 编辑
		async edit(id) {
			// 获取详情
			this.getDetail(id)
			this.getErpOmcProjectTaskList(id)
		},
		// 获取详请
		async getDetail (id) {
			erpOmcTenderResultQueryById({ id }).then(async res => {
				if (res.success) {
					this.cust_lxr = await 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + res.result.ownerUnit + '\'';
					this.model = await {...res.result};
					if (this.model.tenderId) {
						http.get('/api/erp/omc/tender/erpOmcTenderFinal/queryById', { id: this.model.tenderId }).then(async(res) => {
							if (res.success) {
								projectId = res.result.prepareProjectId
							}
						})
					}
					this.model.year = this.model.year + ''
					this.$nextTick(() => {
						this.model.ownerLinkman = res.result.ownerLinkman
						this.model.ownerTel = res.result.ownerTel
					})
				}
			})
		},
		
		// 获取流程列表
		getProcessList (id) {
			if (this.isSubmit) {
				http.get('/api/erp/omc/contractmanager/erpOmcContractManagerFinal/totalCount', { tenderFinalCode: this.model.tenderFinalCode }).then(res => 				{
					if (res.success) {
						if (res.result <= 0){
							//校验通过
							this.$refs.processConfig.showPopup = true
							this.$refs.processConfig.formUrl = this.formUrl
							this.$refs.processConfig.formUrlMobile = this.formUrlMobile
							this.$refs.processConfig.flowCode = this.flowCode
							this.$refs.processConfig.edit(id, this.processId, this.beanName)
						}else{
							//校验不通过
							this.$refs.uToast.show({
								message: '该项目已签收款合同，无法进行投标!',
								complete () {
									if (getCurrentPages().length > 1) {
										uni.navigateBack()
									} else {
										uni.reLaunch({
											url: '/pages/home/home'
										})
									}
									
								}
							})
						}
					} else {
						this.$refs.uToast.show({
							message: res.message,
							complete () {
								if (getCurrentPages().length > 1) {
									uni.navigateBack()
								} else {
									uni.reLaunch({
										url: '/pages/home/home'
									})
								}
								
							}
						})
					}
				})
			}
		},
		
		// 获取分类信息列表
		getErpOmcProjectTaskList (id) {
			queryByIdTenderResultList({ id: id }).then(res => {
				if (res.success) {
					this.dataSource = res.result.records || res.result
				}
			})
		},
		
		// 投标部门详情
		ontenderDeptDetail(item) {
		  var title = ''
		  if (item && item.length) {
		    item.map((value, index) => {
		      title = value.departName + ',' + title
		    })
		    title = title.substring(0, title.lastIndexOf(','))
		  }
		  this.model.proposerDeptText = title
		  
		},
		
		// 点击投标档案
		prepareProjectClick () {
			this.isFinalProjectId = true
		},
		
		// 点击业主单位
		onCustomerClick () {
			this.isCustomerId = true
		},
		
		
		// 投标档案选择
		async tenderFinalChange(tenderFinal) {
			if (!this.isFinalProjectId) return false
			this.cust_lxr = 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + tenderFinal.ownerUnit + '\'';
			projectId = tenderFinal.prepareProjectId
			this.$set(this.model,'bidOpeningData',tenderFinal.tenderEndDate)
			this.$set(this.model,'undertaker',tenderFinal.undertaker)
			this.$set(this.model,'year',tenderFinal.year)
			this.$set(this.model,'tenderFinalCode',tenderFinal.finalNo)
			this.$set(this.model, 'projectName', tenderFinal.projectName)
			this.$set(this.model, 'projectType', tenderFinal.projectType)
			this.$set(this.model,'projectAddress',tenderFinal.projectAddress)
			this.$set(this.model,'detailedAddress',tenderFinal.detailedAddress)
			this.$set(this.model,'ownerUnit',tenderFinal.ownerUnit)
			this.$set(this.model,'ownerLinkman',tenderFinal.ownerLinkman)
			this.$set(this.model,'businessTeam',tenderFinal.businessTeam)
			this.$set(this.model,'businessCompanyId',tenderFinal.businessCompanyId)
			this.$set(this.model,'ownerTel',tenderFinal.ownerTel)
			this.$set(this.model,'biddingMethod',tenderFinal.biddingMethod)
			this.$set(this.model,'tenderContent',tenderFinal.tenderContent)
			this.$set(this.model,'tenderFile',tenderFinal.tenderFile)
			this.$set(this.model,'controlPrice',tenderFinal.controlPrice)
			// BUG#3276
			this.$set(this.model,'projectPublicity',tenderFinal.projectPublicity)

			//todo 迭代11 根据投标档案带出数据
			this.$set(this.model,'tenderUnit',tenderFinal.tenderUnit)
			this.$set(this.model, 'tenderAgent', tenderFinal.tenderFileFinal && tenderFinal.tenderFileFinal.tenderAgent?tenderFinal.tenderFileFinal.tenderAgent : '') // 招标代理机构
			this.$set(this.model, 'tenderAgentLinkman', tenderFinal.tenderFileFinal && tenderFinal.tenderFileFinal.tenderAgentLinkman ? tenderFinal.tenderFileFinal.tenderAgentLinkman 
			: '') // 招标代理联系人
			this.$set(this.model, 'tenderAgentTel', tenderFinal.tenderFileFinal && tenderFinal.tenderFileFinal.tenderAgentTel? tenderFinal.tenderFileFinal.tenderAgentTel : '') // 招标代理联系方式

			// 查询投标单位
			queryTenderListByTenderId({ id: tenderFinal.id }).then((res) => {
				if (res.success) {
				  this.dataSource = res.result || []
				  this.isFinalProjectId = false
				}
			})
			this.$refs.ErpOmcProjectApplyForm.clearValidate() // 清空校验结果
		},
		
		// 添加
		add() {
			this.getGeneratedFormNumber()
			this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 申请时间
			this.model.proposer = uni.getStorageSync('userInfo').empId; //申请人
			this.model.proposerDeptId = uni.getStorageSync('userInfo').deptId; //申请部门
			projectId = ''
		},
		// 获取单据编号
		getGeneratedFormNumber() {
			generatedFormNumber({ prefix: 'TBJG', subSystemCode: '05' }).then(res => {
				if (res.success) {
					this.$set(this.model, 'bidResultNo', res.result)
				}
			})
		},
		
		// 选择业主单位
		onCustomerChange () {
			if (!this.isCustomerId) return false
			this.cust_lxr = 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + this.model.ownerUnit + '\'';
			this.$set(this.model, 'ownerLinkman', '')
			this.$set(this.model, 'ownerTel', '')
			this.isCustomerId = true
		},
		// 选择联系人
		lxrChange (val) {
			var params = {id: val}
			this.$set(this.model, 'ownerLinkman', val)
			erpOmcCustomerManagerContactId(params).then(res => {
				if (res.success) {
					let data = res.result.records || res.result
					this.$set(this.model, 'ownerTel', data.lxfs)
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		
		onInforList (item) {
			this.dataSource = item
		},
		
		// 判断项目名称是否重复
		async isProjectName () {
			let params = await {
				projectName: this.model.projectName,
				projectId: projectId,
				type: 'tenderResult' // 立项：project  预立项：prepare 投标文件审核：tenderFile  投标结果申请：tenderResult
			}
			return http.get('/api/erp/omc/project/erpOmcProject/checkProjectDuplicate', params).then(async res => {
				// 如果验证不通过，需要在callback()抛出new Error('错误提示信息')
				if (res.success) {
					return true
					// return Promise.reject(res.message)
				} else {
					return false
				}
			}).catch(async err => {
				return false
				// return Promise.reject(err.message)
			})
		},
		
		validateForm() {
			return this.$refs.ErpOmcProjectApplyForm.validate().then(async () => {
				var that = this
				if (this.model.bidResult == '1') {
					if (this.dataSource && this.dataSource.length) {
						var result = this.dataSource.some(item => item.bidResult == '1')
						if (result) {
							this.$refs.uToast.show({
								message: '外部单位与内部单位只可有一个中标',
								type: 'error'
							})
							return Promise.reject('外部单位与内部单位只可有一个中标')
						}
					}
				}
				var isProjectName = await this.isProjectName()
				if (!isProjectName) {
					return Promise.reject('项目名称未校验通过')
				}
				return http.get('/api/erp/omc/tender/erpOmcTenderFileFinal/countBytenderid', { tenderId: that.model.tenderId }).then(async(res) => {
					if (res.success) {
						if (res.result > 0) {
							return Promise.resolve()
						} else {
							this.$refs.uToast.show({
								message: '请先添加投标文件审核!',
								type: 'error'
							})
							return Promise.reject('请先添加投标文件审核!')
						}
					} else {
						this.$refs.uToast.show({
							message: '请求错误，请稍后再试!',
							type: 'error'
						})
						return Promise.reject('请求错误，请稍后再试!')
					}
				})
			})
		},
		// 点击提交
		async onSubmit () {
			var that = this
			await this.validateForm()
			this.isSubmit = true
			if(that.model.tenderDeptName&&that.model.tenderDeptName!=null){
				that.model.tenderUnit = that.model.tenderDeptName;
			}
			this.model.resultUnitList = this.dataSource;
			let httpurl = '';
			if (!this.id) {
				httpurl = erpOmcTenderResultAdd;
			} else {
				httpurl = erpOmcTenderResultEdit;
			}
			this.model.draftFlag = true
			httpurl(this.model).then(res => {
				if (res.success) {
					this.$refs.uToast.show({
						message: res.message,
						type: 'default'
					})
					var that = this
					// 获取流程列表
					if (this.id) {
						this.getProcessList(this.id)
					} else {
						// 判断添加的时候是否有返回id值
						if (res.result) {
							this.getProcessList(res.result)
						}
						
					}
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
					this.isSubmit = false
				}
			})
		},
		
		onDrafts() {
			var that = this
			this.isSubmit = true
			if(that.model.tenderDeptName&&that.model.tenderDeptName!=null){
				that.model.tenderUnit = that.model.tenderDeptName;
			}
			this.model.resultUnitList = this.dataSource;
			this.model.draftFlag = false
			let httpurl = '';
			if (!this.id) {
				httpurl = erpOmcTenderResultAdd;
			} else {
				httpurl = erpOmcTenderResultEdit;
			}
			httpurl(this.model).then(res => {
				if (res.success) {
					var that = this
					this.$refs.uToast.show({
						message: res.message,
						type: 'default',
						complete () {
							that.isSubmit = false
							if (getCurrentPages().length > 1) {
								uni.navigateBack()
							} else {
								uni.reLaunch({
									url: '/pages/home/home'
								})
							}
							
						}
					})
					
					
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
					this.isSubmit = false
				}
			})
		},
		
		// 提交流程成功
		onOk () {
			this.$refs.uToast.show({
				message: '提交成功',
				type: 'success',
				complete () {
					if (getCurrentPages().length > 1) {
						uni.navigateBack()
					} else {
						uni.reLaunch({
							url: '/pages/home/home'
						})
					}
					
				}
			})
		}
	}
};
</script>

<style lang="scss" scoped>
@import url("@/assets/inputStyle.scss");
</style>